package com.meisupic.copyright.support;

import com.alibaba.fastjson.JSON;
import com.meisupic.copyright.exception.BizException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Author lanjian
 * @Date 2018/4/26 下午2:19
 */
public class HttpRequester {

  private static Logger logger = LoggerFactory.getLogger(HttpRequester.class);

  public static Response sendPost(String requestUrl, Map<String, String> params) {

    HttpURLConnection urlConnection = null;

    try {
      URL url = new URL(requestUrl);
      urlConnection = (HttpURLConnection) url.openConnection();
      urlConnection.setRequestMethod("POST");
      urlConnection.setDoOutput(true);
      urlConnection.setDoInput(true);
      urlConnection.setUseCaches(false);
      urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
      urlConnection.setRequestProperty("Connection", "Keep-Alive");// 维持长连接
      urlConnection.setRequestProperty("Charset", "UTF-8");

      List<NameValuePair> pairList = new ArrayList<NameValuePair>(params.size());
      for (Map.Entry<String, String> entry : params.entrySet()) {
        NameValuePair pair = new BasicNameValuePair(entry.getKey(), entry.getValue());
        pairList.add(pair);
      }
      UrlEncodedFormEntity requestentity = new UrlEncodedFormEntity(pairList,
          Charset.forName("UTF-8"));
      urlConnection.getOutputStream().write(IOUtils.toByteArray(requestentity.getContent()));
      urlConnection.getOutputStream().flush();
      urlConnection.getOutputStream().close();

      int statusCode = urlConnection.getResponseCode();
      InputStream in = urlConnection.getInputStream();
      String conent = IOUtils.toString(in,"utf-8");
      Response response = new Response();
      response.setContent(conent);
      response.setStatusCode(statusCode);
      return response;
    } catch (IOException e) {
      logger.error("post请求异常",e);
      throw new BizException("post请求异常");
    }

  }


  public static Response sendPost(String requestUrl, String body) {

    HttpURLConnection urlConnection = null;

    try {
      URL url = new URL(requestUrl);
      urlConnection = (HttpURLConnection) url.openConnection();
      urlConnection.setRequestMethod("POST");
      urlConnection.setDoOutput(true);
      urlConnection.setDoInput(true);
      urlConnection.setUseCaches(false);
      urlConnection.setRequestProperty("Content-Type", "application/json");
      urlConnection.setRequestProperty("Connection", "Keep-Alive");// 维持长连接
      urlConnection.setRequestProperty("Charset", "UTF-8");

      StringEntity requestentity = new StringEntity(body, Charset.forName("UTF-8"));

      urlConnection.getOutputStream().write(IOUtils.toByteArray(requestentity.getContent()));
      urlConnection.getOutputStream().flush();
      urlConnection.getOutputStream().close();

      int statusCode = urlConnection.getResponseCode();
      InputStream in = urlConnection.getInputStream();
      String conent = IOUtils.toString(in,"utf-8");
      Response response = new Response();
      response.setContent(conent);
      response.setStatusCode(statusCode);
      return response;
    } catch (IOException e) {
      logger.error("post请求异常",e);
      throw new BizException("post请求异常");
    }

  }

  public static void main(String[] args) throws IOException {

    Map<String, String> params = new HashMap<>();
    params.put("json", "{}");
    params.put("proxySign", "11");
    params.put("applyerSign", "112");
    params.put("eCertId", "32332");
    Response response = HttpRequester.sendPost("http://210.14.147.107:9000/DWCRServer/contractPlugin/contractforesignbyseller.do",params);
    System.out.println(JSON.toJSONString(response));
  }
}
